package com.jack.vipmis.vip.api.web.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.jack.vipmis.common.web.controller.BaseController;
import com.jack.vipmis.vip.adv.entity.Advertising;
import com.jack.vipmis.vip.adv.service.AdvertisingService;
import com.jack.vipmis.vip.api.util.MsgUtil;

/**
 * 广告API
 * 
 * @author JackLiang
 * @version 1.0
 * @date 2014-6-30
 */
@Controller
@RequestMapping(value = "api/adv")
public class AdvAPIController extends BaseController<Advertising, Long> {

	@Autowired
	private AdvertisingService	advertisingService;

	/**
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "show", method = RequestMethod.GET)
	public String showAd(@RequestParam("id") long id, Model model) {
		Advertising m = advertisingService.findOne(id);
		if (m != null) {
			model.addAttribute("dataMap", MsgUtil.doneOk(m));
		} else {
			model.addAttribute("dataMap", JSON.toJSON(MsgUtil.doneMsg(MsgUtil.OK, "广告信息不存在!", m)));
		}
		return viewName("infomation");
	}

	/**
	 * 获取广告信息
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "findByType", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> findAdvByType(@RequestParam("type") int type) {
		try {
			Advertising m = advertisingService.findByType(type);
			if (m != null) {
				return MsgUtil.doneOk(m);
			} else {
				return MsgUtil.doneMsg(MsgUtil.OK, "该广告类型不存在!", null);
			}
		} catch (Exception e) {
		}
		return MsgUtil.doneFail();
	}

	@RequestMapping(value = "list")
	@ResponseBody
	public Map<String, Object> getAdvList(@RequestParam("type") int type) {
		try {
			List<Advertising> list = advertisingService.list(type);
			if (!list.isEmpty()) {
				return MsgUtil.doneOk(list);
			} else {
				return MsgUtil.doneMsg(MsgUtil.OK, "广告信息不存在", null);
			}
		} catch (Exception e) {
		}
		return MsgUtil.doneFail();
	}
}
